package view.rules;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ontology.Concept;
import ontology.ConceptList;
import ruletree.rule.Action;
import ruletree.rule.Rule;

import components.Component;


import lexicon.Form;
import lexicon.Lexicon;
import lexicon.LexiconList;
import managers.OntologyManager;

import net.miginfocom.swing.MigLayout;

public class SpelledOutPanelD extends JPanel{
	
	SubmitPanel submitPanel;
	FeaturePane featurePane;
	private JPanel secondPanel;
	private JLabel baseFormLabel;
	private JComboBox<String> baseFormCombo;
	private JButton featuresButton;
	private JLabel prefixLabel;
	private JTextField prefixTextField;
	private JLabel glossLabel;
	private JTextField glossTextField;
	private JLabel prefixTagLabel;
	private JComboBox prefixTagCombo;
	private JLabel commentLabel;
	private JTextArea commentArea;
	private LexiconList lexiconList;
	private String posFeatures;
	FeatureButtonListener featureButtonListener;
	private OntologyManager ontologyManager;
	private ConceptList conceptList;
	private String ruleName;
	public SpelledOutPanelD(LexiconList list){
		
		this.setLayout(new MigLayout());
		lexiconList = list;
		initComponents();	
		addComponents();
	}
	
	public SpelledOutPanelD(){
		this.setLayout(new MigLayout());
		//lexiconList = list;
		initComponents();	
		addComponents();
	}
	
	private void initComponents(){
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		secondPanel = new JPanel();
		secondPanel.setLayout(new MigLayout());
		baseFormLabel = new JLabel("Base Form:");
		baseFormCombo = new JComboBox();
		baseFormCombo.setMinimumSize(new Dimension(200, 20));
		featuresButton = new JButton("Features");

		 featuresButton.addActionListener(new ActionListener() {
         	public void actionPerformed(ActionEvent e) {
         		featureButtonListener.setPOS(posFeatures);
         		featurePane.setTextArea(featureButtonListener.getFeatures());
				
         	}
         });

     	featureButtonListener = new FeatureButtonListener();
         featuresButton.addActionListener(featureButtonListener);

		featurePane = new FeaturePane();
	    featurePane.setMinimumSize(new Dimension((int)screenSize.getWidth() - 500, 80));
	    
	    commentLabel = new JLabel("Comment:");
	    commentArea = new JTextArea();
	    commentArea.setMinimumSize(new Dimension((int)screenSize.getWidth() - 200, (int)screenSize.getHeight() - 530));
		
	    submitPanel = new SubmitPanel();
	    submitPanel.okButton.addActionListener(new btnSaveRuleListener());
	}
	
	private void addComponents(){
		
		addToMidPanel();
		add(secondPanel, "wrap 20");
		add(commentLabel, "wrap");
		add(commentArea, "wrap 40");
		add(submitPanel);
		
		
	}
	
	private void addToMidPanel(){
		secondPanel.add(baseFormLabel);
		secondPanel.add(baseFormCombo, "wrap");
		secondPanel.add(featuresButton);
		secondPanel.add(featurePane);
	}
	
	

	public void setFormModel(LexiconList Lexlist){
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel();
		ArrayList<String> list = Lexlist.getPossibleForms();
		for(String p: list){
			model.addElement(p);
			System.out.println("IM in PanelD " + p);
		}
		baseFormCombo.setModel(model);
		//return model;
		
	}

	public LexiconList getLexiconList() {
	
		return lexiconList;
	}

	public void setLexiconList(LexiconList lexiconList) {
		this.lexiconList = lexiconList;
		ArrayList<Lexicon> list = new ArrayList<Lexicon>();
		ArrayList<String> listForm = lexiconList.getPossibleForms();
		DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
		for(Lexicon l: list){
			model.addElement(l.getName());
		}
		
		baseFormCombo.setModel(model);
	}

	public String getPosFeatures() {
		return posFeatures;
	}

	public void setPosFeatures(String posFeatures, String ruleName) {
		this.posFeatures = posFeatures;
		this.ruleName = ruleName;
	}
	
	
	public void getRule(){
		Rule rule = new Rule(ruleName);
		Component left = Component.createInstance(getPosFeatures(), true);
		left.setVar("$1");
		rule.addChildren(left);
		Action action = new Action();
		action.addFeature(featurePane.getFeatureList(), "$1");
		rule.addAction(action);
	
		File file = new File("Languages\\Filipino\\RuleTree\\Structural Adjustment rule\\SpellD.xml");
		try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rule.saveRule(file);
		
	
	}
	
	
	class btnSaveRuleListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try{
				getRule();
			} catch(Exception f){
				f.printStackTrace();
				JOptionPane.showMessageDialog(null, "Error");
			}
		}
		
	}

}
